package com.yanqu.road.server.logic.activity.condition.newpatronsfund;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eQualityType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserCombLeaderData;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fund.ActivityFundModule;
import com.yanqu.road.server.gameplayer.module.patrons.CombLeaderModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;

import java.math.BigInteger;
import java.util.List;

public class NewPatronsFundBeautyChCondition extends BaseActivityCondition {
    public NewPatronsFundBeautyChCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        this.doChangeVal();
    }

    /**
     * 执行值变更
     */
    protected void doChangeVal() {
        //获取门客信息
        int patronsId = Integer.parseInt(this.activityInfo.getParam6());
        UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
        if (userPatrons == null) {
            return;
        }
        //判断门客是否无双
        int quality = PatronsMgr.getPatronsQuality(userPatrons);
        if(quality < eQualityType.Red.getValue()){
            return;
        }
        //获取美女信息
        int relationBeautyId = this.activityConditionInfo.getParamList().get(1).intValue();//门客关联的美女ID
        UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(relationBeautyId);
        if (userBeauty != null) {
            //美女总才华
            long glamour = userBeauty.getTotalGlamour();
            BigInteger val = BigInteger.valueOf(glamour);
            if(val.compareTo(getValue()) > 0) {
                setValue(val);
            }
        }
    }

    private boolean checkBuyMallInfo() {
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        int mallKey = paramList.get(0).intValue();
        if (mallKey != 0) {
            // 付费
            MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(activityConditionInfo.getActivityId(), mallKey);
            if (mallInfo == null) {
                return false;
            }
            return player.getModule(ActivityFundModule.class).isBuy(mallInfo);
        }
        return true;
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        if (!checkBuyMallInfo()) {
            // 付费
            return false;
        }
        if (paramList.get(3).compareTo(getValue()) <= 0) {
            return true;
        }
        return false;
    }

    @Override
    public void initDefaultValue() {
        this.doChangeVal();
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.NewPatronsFundBeautyCh.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.NewPatronsFundBeautyCh.getValue(), handle);
    }
}
